package com.free.web.controller.system.controller;

import com.free.common.annotation.Log;
import com.free.common.core.controller.BaseController;
import com.free.common.core.domain.AjaxResult;
import com.free.common.core.page.TableDataInfo;
import com.free.common.enums.BusinessType;
import com.free.common.utils.poi.ExcelUtil;
import com.free.system.domain.FreeTicket;
import com.free.system.service.IFreeTicketService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 票券Controller
 *
 * @author Sophia
 * @date 2025-02-27
 */
@Controller
@RequestMapping("/system/ticket")
public class FreeTicketController extends BaseController {
    private String prefix = "system/ticket";

    @Autowired
    private IFreeTicketService freeTicketService;

    @RequiresPermissions("system:ticket:view")
    @GetMapping()
    public String ticket() {
        return prefix + "/ticket";
    }

    /**
     * 查询票券列表
     */
    @RequiresPermissions("system:ticket:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FreeTicket freeTicket) {
        startPage();
        List<FreeTicket> list = freeTicketService.selectFreeTicketList(freeTicket);
        return getDataTable(list);
    }

    /**
     * 获取全部票券信息
     * @return
     */
    @GetMapping("/getTicketList")
    @ResponseBody
    public AjaxResult getList() {
        List<FreeTicket> list = freeTicketService.selectFreeTicketList(null);
        return AjaxResult.success(list);
    }

    /**
     * 导出票券列表
     */
    @RequiresPermissions("system:ticket:export")
    @Log(title = "票券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FreeTicket freeTicket) {
        List<FreeTicket> list = freeTicketService.selectFreeTicketList(freeTicket);
        ExcelUtil<FreeTicket> util = new ExcelUtil<FreeTicket>(FreeTicket.class);
        return util.exportExcel(list, "票券数据");
    }

    /**
     * 新增票券
     */
    @RequiresPermissions("system:ticket:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存票券
     */
    @RequiresPermissions("system:ticket:add")
    @Log(title = "票券", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FreeTicket freeTicket) {
        return toAjax(freeTicketService.insertFreeTicket(freeTicket));
    }

    /**
     * 修改票券
     */
    @RequiresPermissions("system:ticket:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        FreeTicket freeTicket = freeTicketService.selectFreeTicketById(id);
        mmap.put("freeTicket", freeTicket);
        return prefix + "/edit";
    }

    /**
     * 修改保存票券
     */
    @RequiresPermissions("system:ticket:edit")
    @Log(title = "票券", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FreeTicket freeTicket) {
        return toAjax(freeTicketService.updateFreeTicket(freeTicket));
    }

    /**
     * 删除票券
     */
    @RequiresPermissions("system:ticket:remove")
    @Log(title = "票券", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(freeTicketService.deleteFreeTicketByIds(ids));
    }
}
